package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.query.ObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //用于跳转到部门页面
    @RequiresPermissions("department:index")
    @PermissionName("部门主页")
    @RequestMapping("index")
    public String index() {
        return "department/list";
    }


    @RequestMapping("list")
    @RequiresPermissions("department:list")
     @PermissionName("部门列表")
    @ResponseBody
    public PageResult list(@ModelAttribute("qo") ObjectQuery qo) {
        //返回json格式的查询结果对象
        PageResult<Department> list = departmentService.query(qo);
        return list;
    }

        @RequiresPermissions("department:saveOrUpdate")
    @PermissionName("部门创建更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Model model, Department department) {
        JsonResult json = new JsonResult();
        try {
            if (department.getId() != null) {
                departmentService.update(department);
            } else {
                departmentService.save(department);

            }
        } catch (Exception e) {
            e.printStackTrace();
            json.markErrorMsg("更新操作失败!");
        }
        return json;
    }

    //用于修改部门状态
//用于跳转到部门页面
    @RequestMapping("changeState")
    @ResponseBody
    public void changeState(Long id) {
        departmentService.changeState(id);
    }


    //部门删除
    @RequiresPermissions("department:delete")
    @PermissionName("部门删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                departmentService.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("删除部门失败");
        }
        return jsonResult;
    }

    //查找所有部门

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Department> selectAll() {
        List<Department> list = departmentService.getAll();
        return list;
    }


//
//    @RequestMapping("input")
//    public String input(Model model, Long id) {
//        if (id != null) {
//
//            model.addAttribute("department", departmentService.get(id));
//        }
//        return "department/input";
//    }
//


}
